package com.example.tianyutong.qanda.ui.page_4;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.example.tianyutong.qanda.R;
import com.example.tianyutong.qanda.base.BaseFragment;
import com.example.tianyutong.qanda.databinding.FragmentContentBinding;
import com.example.tianyutong.qanda.ui.page_1.child.ListFragment;
import com.example.tianyutong.qanda.view.MyFragmentPagerAdapter;
import com.example.tianyutong.qanda.viewmodel.menu.NoViewModel;

import java.util.ArrayList;

public class Fragment_me extends BaseFragment<NoViewModel, FragmentContentBinding> {
    //这几个大的fragment只用来显示标签，用的都是fragment_content,而child文件夹里的小fragment对应着每个标签下的界面才有界面，initFragmentList里添加
    private ArrayList<String> mTitleList = new ArrayList<>(3);
    private ArrayList<Fragment> mFragments = new ArrayList<>(3);
    private boolean mIsFirst = true;
    private boolean mIsPrepared;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mIsPrepared = true;

    }

    @Override
    protected void loadData() {
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }
        bindingView.includeMe.getRoot().setVisibility(View.VISIBLE);//点击个人资料时显示头像栏
        showLoading();
        bindingView.vpGank.postDelayed(() -> {
            initFragmentList();
            MyFragmentPagerAdapter myAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitleList);
            bindingView.vpGank.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
            // 左右预加载页面的个数
            bindingView.vpGank.setOffscreenPageLimit(3);
            bindingView.tabGank.setupWithViewPager(bindingView.vpGank);
            // item点击跳转
            showContentView();
            mIsFirst = false;
        }, 130);
    }

    @Override
    public int setContent() {
        return R.layout.fragment_content;
    }

    private void initFragmentList() {
        mTitleList.clear();
        mTitleList.add("我的问题");
        mTitleList.add("我的回答");
        mTitleList.add("我的收藏");
        mFragments.add(QuestionFragment.newInstance("my"));
        mFragments.add(AnswerFragment.newInstance("Android"));
        mFragments.add(QuestionFragment.newInstance("like"));
    }
}